<?php
require_once('./includes/phpunit_setup.inc');

class kservices_module_TestCase extends DrupalTddTestCase{

  var $admin_user;
  var $web_user;
  var $anon_user;
  var $node;

  function setup() {
    parent::setup();
    drupal_install_modules(array('kasahorow', 'kservices', 'kdevel', 'devel', 'kentry'));
  }
  
  public function testAllowedSyncKeysSetting() {
    variable_set('kservices_sync_keys', 'appengine');
    $keys = _kservices_allowed_sync_keys();
    $this->assertEquals(array('appengine'), $keys);
    
    variable_set('kservices_sync_keys', 'key1
                 key2');
    $keys = _kservices_allowed_sync_keys();
    $this->assertEquals(array('key1', 'key2'), $keys,
                        'Keys should be separated by newlines');
  }

  public function testStrictDupChecking() {
    $node1 = array('nid' => 1, 'title' => 'A title'); 
    $node2 = array('nid' => 1, 'title' => 'A title'); 
    $this->assertTrue(_knodes_equal($node1, $node2, array('nid', 'title')));
    $node2['nid'] = 2;  # Change the nid
    $this->assertFalse(_knodes_equal($node1, $node2, array('nid', 'title')));
    $this->assertTrue(_knodes_equal($node1, $node2, array('title')));
  }

  public function testAPINameRemap() {
    $this->assertEquals(_translate_api_field_name('db'), 'k.iso');
    $this->assertEquals(_translate_api_field_name('sample'), 'k.example');
    $this->assertEquals(_translate_api_field_name('group'), 'k.word');
    $this->assertEquals(_translate_api_field_name('body'), 'k.defn');
    $this->assertEquals(_translate_api_field_name('pcat'), 'k.partofspeech');
  }

  public function testSaveGetNode() {
    $auth = array('key' => '401082052');
    $_node = array(
      'title' => 'Test',
    );
    $nid = _save_node($auth, $_node);
    $node = _get_node($auth, (int) $nid);
    $this->assertEquals($_node['title'], $node['title']);
  }
}
